import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.reindex.BulkByScrollResponse;
import org.elasticsearch.index.reindex.DeleteByQueryAction;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.net.InetAddress;
import java.net.UnknownHostException;

import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;


/**
 * Created by heliang on 2018/5/23.
 */

public class ESClientTest {

    private Client client = null;

    private String index = "bank";
    private String type = "extrnal";


    @Before
    public void init() throws UnknownHostException {
        // on startup
        Settings settings = Settings.builder().put("client.transport.ignore_cluster_name", "true").build();

         client = new PreBuiltTransportClient(settings)
                .addTransportAddress(new TransportAddress(InetAddress.getByName("10.80.10.63"), 9500));

        System.out.println("client -->" + client);
    }

    @After
    public void shutdown() {
        // on shutdown
        client.close();
    }

    /**
     * 查询
     * @throws UnknownHostException
     */
    @Test
    public void testGet() throws UnknownHostException {
        GetResponse getResponse = client.prepareGet("bank", "extrnal", "1").get();
        System.out.println(getResponse.getSource());
    }

    /**
     * 删除方法
     */
//    @Test
    public void delete() {
        try {
            DeleteResponse response = client.prepareDelete("bank", "tweet", "1").get();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

        }
    }


    /**
     * 查询后批量删除
     */
//    @Test
    public void deleteByQuery() {
        BulkByScrollResponse response = DeleteByQueryAction.INSTANCE.newRequestBuilder(client)
                .filter(QueryBuilders.matchQuery("gender", "male"))
                .source("persons")
                .get();
        long deleted = response.getDeleted();
    }

    /**
     * 更新操作
     */
    @Test
    public void update() {
        try {
            UpdateRequest updateRequest = new UpdateRequest();
            updateRequest.index("bank");
            updateRequest.type("extrnal");
            updateRequest.id("2");
            updateRequest.doc(jsonBuilder()
                    .startObject()
                    .field("gender", "male")
                    .endObject());
            client.update(updateRequest).get();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {

        }
    }

    /**
     * 如果有则更新，没有则插入
     */
    @Test
    public void upsert() {

        try {
            IndexRequest indexRequest = new IndexRequest("bank", "extrnal", "2")
                    .source(jsonBuilder()
                            .startObject()
                            .field("name", "Joe Smith")
                            .field("gender", "male")
                            .endObject());

            UpdateRequest updateRequest = new UpdateRequest("bank", "extrnal", "2")
                    .doc(jsonBuilder()).upsert(indexRequest);
            client.update(updateRequest).get();

            System.out.println("success");

        } catch (Exception e) {

        } finally {

        }


    }

}
